package com.topvision.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topvision.common.response.SuccessResponse;
import com.topvision.common.security.Md5Util;
import com.topvision.common.security.UserInfo;
import com.topvision.common.security.UserInfoUtils;
import com.topvision.domain.Device;
import com.topvision.service.DeviceInfoService;
import com.topvision.service.DeviceService;
import com.topvision.utils.Const;
import com.topvision.utils.HttpClientUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/device")
@Api(value = "个人设备信息", description = "智能眼镜相关操作")
@Slf4j
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private UserInfoUtils userInfoUtils;

    @GetMapping("/deviceList")
    public ResponseEntity deviceList(Principal principal) {
        UserInfo userInfo = userInfoUtils.getUserInfo(principal);
        List<Device> deviceList = deviceService.findByCreatedBy(userInfo.getName());
       // List<Device> deviceDTOS = deviceService.findByUserName(userInfo.getName());
        return ResponseEntity.ok(SuccessResponse.of(deviceList));
    }

    @PostMapping("/addDevice")
    public ResponseEntity addDevice(@RequestBody Device device) {
        if (device != null && device.getBtMac() != null) {
            deviceService.save(device);
        }
        return ResponseEntity.ok(SuccessResponse.of(device));
    }

    @PostMapping("/editDevice")
    public ResponseEntity editDevice(@RequestBody Device device) {
        if (device != null && device.getBtMac() != null) {
            deviceService.save(device);
        }
        return ResponseEntity.ok(SuccessResponse.of(device));
    }

    @GetMapping("/delDevice/{deviceId}")
    public ResponseEntity delDevice(@PathVariable String deviceId) {
        if (deviceId != null) {
            deviceService.delete(Long.valueOf(deviceId));
        }
        return ResponseEntity.ok(SuccessResponse.of());
    }

    @GetMapping("/createRemoteAssistenceNo/{btmac}")
    public ResponseEntity createRemoteAssistenceNo(@PathVariable String btmac) {

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("companyid", "101");
        map.put("deviceid", btmac);//设备蓝牙地址
        map.put("devicetype", "301");
        map.put("devicevendorid", "3");


        StringBuffer sb = new StringBuffer();
        sb.append("companyid=").append("101").append("&deviceid=").append(map.get("deviceid")).append("&devicetype=301").append("&devicevendorid=3");
        sb = new StringBuffer(Md5Util.getMd5("MD5", 0, null, sb.toString()));
        sb.append("123456");
        String digest = Md5Util.getMd5("MD5", 0, null, sb.toString());
        map.put("digest", digest);
        String result = HttpClientUtil.getZwanPostResult(Const.ZWAN_URL + "zwgw/createdevice", map);
        if (result != null && !result.equals("")) {
            JSONObject json = JSON.parseObject(result);
            String remoteNo = json.getString("zwannumber");
            String pwd = json.getString("password");
            if (remoteNo != null && !remoteNo.equals("")) {
                Device device = deviceService.findByBtMac(btmac);
                device.setRemoteAssistanceNo(remoteNo);
                device.setRemoteAssitancePwd(pwd);
                deviceService.save(device);
                return ResponseEntity.ok(SuccessResponse.of(device));
            }
        }
        return ResponseEntity.ok(SuccessResponse.of());
    }
}
